rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(estimatr)
library(texreg)

load('./data/panel_month_dummies.RData')

# Table H1: Covid-19 Unemployment Effects on Asian Hate Crimes by Prejudice

out_econ_simple_het_vshare <- lm_robust(hc_pc_asians ~ covid2*losing_income_d*extr_right_d,
                                        data=panel,
                                        se_type='stata',
                                        clusters = panelvar)

if (!file.exists('./output/out_econ_het_vshare.RData') &
    !file.exists('./output/out_econ_het_vshare_con.RData') &
    !file.exists('./output/out_econ_het_vshare_con_trend.RData') &
    !file.exists('./output/out_econ_het_vshare_con_trend_all.RData')) {
  source('./code/tableH1reg.R')
}

out_econ_het_vshare <- get(load('./output/out_econ_het_vshare.RData'))
out_econ_het_vshare_con <- get(load('./output/out_econ_het_vshare_con.RData'))
out_econ_het_vshare_con_trend <- get(load('./output/out_econ_het_vshare_con_trend.RData'))
out_econ_het_vshare_con_trend_all <- get(load('./output/out_econ_het_vshare_con_trend_all.RData'))

tableh1 <- texreg(list(out_econ_simple_het_vshare, out_econ_het_vshare, out_econ_het_vshare_con, out_econ_het_vshare_con_trend, out_econ_het_vshare_con_trend_all),
       include.ci = FALSE,
       include.adjrs = FALSE, include.rmse = FALSE,
       stars = c(0.001, 0.01, 0.05, 0.1), digits = 5,
               custom.coef.map = list('losing_income_d' = 'Expected unemployment', 'covid2'= 'After Jan, 2020',
                                      'extr_right_d' = 'High prejudice',
                                      'covid2:extr_right_d' = 'After Jan, 2020 x high prejudice',
                                      'losing_income_d:extr_right_d' = 'Unemployment x high prejudice',
                                      'covid2:losing_income_d' = 'After Jan, 2020 x unemployment',
                                      'covid_econ' = 'After Jan, 2020 x unemployment',
                                      'covid2:losing_income_d:extr_right_d' = 'After Jan, 2020 x unemployment x high prejudice'),
       symbol = "\\circ",
       caption = "Covid-19 Unemployment Effects on Asian Hate Crimes by Prejudice",
       caption.above = TRUE)

write(tableh1, file.path('./tableH1.tex'))

# Table H2: Covid-19 Unemployment Effects on Asian Hate Crimes by Far-right Mayors
out_econ_simple_het <- lm_robust(hc_pc_asians ~ covid2*losing_income_d*extr_right_mayor,
                                 data=panel,
                                 se_type='stata',
                                 clusters = panelvar)

if (!file.exists('./output/out_econ_het.RData') &
    !file.exists('./output/out_econ_het_con.RData') &
    !file.exists('./output/out_econ_het_con_trend.RData') &
    !file.exists('./output/out_econ_het_con_trend_all.RData')) {
  source('./code/tableH2reg.R')
}

out_econ_het <- get(load('./output/out_econ_het.RData'))
out_econ_het_con <- get(load('./output/out_econ_het_con.RData'))
out_econ_het_con_trend <- get(load('./output/out_econ_het_con_trend.RData'))
out_econ_het_con_trend_all <- get(load('./output/out_econ_het_con_trend_all.RData'))

tableh2 <- texreg(list(out_econ_simple_het, out_econ_het, out_econ_het_con, out_econ_het_con_trend, out_econ_het_con_trend_all),
       include.ci = FALSE,
       include.adjrs = FALSE, include.rmse = FALSE,
       stars = c(0.001, 0.01, 0.05, 0.1),
       digits = 5,
               custom.coef.map = list('losing_income_d' = 'Expected unemployment', 'covid2'= 'After Jan, 2020',
                                      'extr_right_mayor' = 'Far-right mayor',
                                      'covid2:extr_right_mayor' = 'After Jan, 2020 x Far-right mayor',
                                      'losing_income_d:extr_right_mayor' = 'Unemployment x Far-right mayor',
                                      'covid2:losing_income_d' = 'After Jan, 2020 x Unemployment',
                                      'covid_econ' = 'After Jan, 2020 x Unemployment',
                                      'covid2:losing_income_d:extr_right_mayor' = 'After Jan, 2020 x Unemployment x Far-right mayor'),
       symbol = "\\circ",
       caption = "Covid-19 Unemployment Effects on Asian Hate Crimes by Far-right Mayors",
       caption.above = TRUE)

write(tableh2, file.path('./tableH2.tex'))